package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.commonutils.result.R;
import com.atguigu.guli.service.edu.entity.Chapter;
import com.atguigu.guli.service.edu.entity.vo.ChapterVo;
import com.atguigu.guli.service.edu.service.ChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author wangyang
 * @since 2021-05-27
 */
@Api(tags = "章节管理")
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("admin/edu/chapter")
public class ChapterController {
    @Autowired
    private ChapterService chapterService;

    @ApiOperation("保存章节信息")
    @PostMapping("/saveChapter")
    public R saveChapter(
            @ApiParam(value = "章节信息",required = true)
            @RequestBody Chapter chapter
            ) {
        boolean result = chapterService.save(chapter);
        if (result) {
            return R.ok().message("添加成功！");
        }else {
            return  R.error().message("添加失败！");
        }
    }

    @ApiOperation("根据Id查询章节信息")
    @GetMapping("/getChapterInfo/{id}")
    public R getChapterById(
            @ApiParam(value = "章节Id",required = true)
            @PathVariable String id
    ) {
        Chapter chapter = chapterService.getById(id);
        if (chapter != null) {
            return R.ok().data("item", chapter);
        } else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("根据id修改章节")
    @PutMapping("updateChapterInfo")
    public R updateById(
        @ApiParam(value="章节对象", required = true)
        @RequestBody Chapter chapter){
        boolean result = chapterService.updateById(chapter);
        if (result) {
            return R.ok().message("修改成功");
        } else {
            return R.error().message("数据不存在");
        }
    }
    @ApiOperation("根据id删除章节")
    @DeleteMapping("/remove/{id}")
    public R deleteById(
            @ApiParam(value = "章节Id",required = true)
            @PathVariable String id
    ) {
        boolean result = chapterService.removeChapterById(id);
        if (result) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("删除失败");
        }
    }

    @ApiOperation("嵌套章节数据列表")
    @GetMapping("nested-list/{courseId}")
    public R nestedList(
            @ApiParam(value = "章节Id",required = true)
            @PathVariable String courseId
    ) {
       List<ChapterVo> chapterVoList = chapterService.nestedList(courseId);
        return R.ok().data("items", chapterVoList);
    }
}

